package com.yangcangclub.message.domain;

import com.yangcangclub.message.utils.SMSUtil;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.utils.StringUtil;
import com.yangcangclub.message.base.MesgBaseDomain;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 短信发送
 */

@Component
public class SMS extends MesgBaseDomain {



    /**
     * 给手机号发送短信
     * @param type 1 开卡验证码 2 开卡成功提示
     * @return
     */

    public boolean sendValidCode(String phone,int type,String cardNum) {

        //校验
//        if(!StringUtil.checkPhoneNo(phone)){
//            throw new ApolloException(MessageEnum.PHONE_FORMAT_ERROR);
//        }

        boolean flag=false;
        String[] contents=new String[2];
        String num= StringUtil.getRandomNum(4);
        if(1==type){
            contents[0]=num;//验证码
            contents[1]="1分钟";//验证码
            flag= SMSUtil.sendSMS(phone, super.sysConfig.getSmsTempId(), contents);
        }else if(2==type){
            contents[0]=cardNum;//验证码
            flag= SMSUtil.sendSMS(phone,super.sysConfig.getSendActiveCardId(),contents);
        }

        /**判断手机号是国内还是国际 **/
//        String prefix  = phone.substring(0, 3);
//        if(StringUtil.isEquals("+86",prefix)){
//            type = 0 ; // 国内
//        }else {
//            type = 1;// 国际
//        }
//        SMSApi.sendSMS(phone,new String[]{num},type);

        if(flag){
            //设置定时缓存
            redisTemplate.boundValueOps("message.phone."+phone+".validcode").set(num, 10, TimeUnit.MINUTES);
        }

        return flag;

    }


    /**
     * 验证code是否正确
     * @param phone  手机号
     * @param code  验证码
     * @return
     */

    public boolean verifyCode(String phone, String code) {

        //校验
        if(!StringUtil.checkPhoneNumber(phone)){
            throw new ApolloException(MessageEnum.PHONE_FORMAT_ERROR);
        }else if(code==null||code.length()>6){
            throw new ApolloException(MessageEnum.VALID_CODE_FORMAT_ERROR);
        }

        if(!redisTemplate.hasKey("message.phone."+phone+".validcode")) {
            throw new ApolloException(MessageEnum.VERIFY_CODE_SHIXIAO_FAIL);
        }

        //查询缓存
       Object obj= redisTemplate.boundValueOps("message.phone."+phone+".validcode").get();

        if(obj!=null&&code.equals(obj)){
            return true;
        }else{
            return false;
        }
    }

    public boolean verifyMailCode(String email, String code) {

        //校验
        if(!StringUtil.checkEmail(email)){
            throw new ApolloException(MessageEnum.MAIL_FORMAT_ERROR);
        }else if(code==null||code.length()>6){
            throw new ApolloException(MessageEnum.VALID_CODE_FORMAT_ERROR);
        }

        if(!redisTemplate.hasKey("message.email."+email+".validcode")) {
            throw new ApolloException(MessageEnum.VERIFY_CODE_SHIXIAO_FAIL);
        }

        //查询缓存
        Object obj= redisTemplate.boundValueOps("message.email."+email+".validcode").get();

        if(obj!=null&&code.equals(obj)){
            return true;
        }else{
            return false;
        }
    }
}
